//
//  InviteCodeInputVC.swift
//  LeGou
//
//  Created by lxm on 2022/7/26.
//

import UIKit

class InviteCodeInputVC: ZMViewController {

    //MARK:--------------------属性--------------------
    // 0 第一次注册 1个人中心过来补填
    var type = 0
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var codeText: UITextField!
    
    @IBOutlet weak var loginButton: UIButton!
    
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "邀请信息"
        setUI()
        addConstraints()
        bindAction()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }
    
    //MARK:--------------------UI--------------------
    func setUI() {
        title = ""
        if type == 0 {
            setLeftBtn(imgStr: "")
            setRightCustomBtn(titleStr: "跳过", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 14))

        } else {
            
        }
    }
    
    func addConstraints() {
        
    }
    
    override func leftBtnClick(sender: UIButton) {
        if type == 1 {
            super.leftBtnClick(sender: UIButton())
        }
    }
    
    override func rightBtnClick(sender: UIButton) {
        setInviteCode(code: nil)
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        codeText.rx.text.orEmpty.map({$0.count >= 6}).subscribe(onNext:{ [unowned self] resp in
            self.loginButton.isEnabled = resp
            self.loginButton.backgroundColor = resp ? Style.mainColor : Style.disableColor
        }).disposed(by: disposeBag)
    }
    
    @IBAction func onScan(_ sender: Any) {
        let vc = SWQRCodeViewController()
        navigationController?.pushViewController(vc, animated: true)
        vc.QRCodeBlock = { [unowned self] value in
            self.codeText.text = value
        }
    }
    
    @IBAction func onSure(_ sender: Any) {
        if codeText.text!.zm.isEmpty() {
            ZMToast.showToast(title: "请填写邀请码")
            return
        }
        setInviteCode(code: codeText.text!)
    }
    
    func setInviteCode(code : String?) {
        ZMToast.showActivity()
        var pa = [String : Any]()
        if let code = code {
            pa["spreadInfo"] = code
        }
        MineAPI.inviteCode(pa).rxRequestNullAble(type: Any.self).subscribe(onNext:{ [unowned self] resp in
            ZMAppShareData.share.requestUserInfo { ok in
                ZMToast.hiddenActivity()
                // 跳到首页了
                self.navigationController?.popToRootViewController(animated: true)
            }
        }).disposed(by: disposeBag)
    }
    
    // 重写返回按钮
    override func customBackButton() -> UIButton? {
        if type == 0 {
            return nil
        } else {
            let btn = ZMUtil.button(title: "返回", image: UIImage.init(named: "back_18"), font: UIFont.systemFont(ofSize: 18), color: .black)
            btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
            btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)
            return btn
        }
    }
    
    
    //MARK:--------------------delegate--------------------

}
